#include "Common.hpp"
#include "Fifo.hpp"

int main()
{
    Fifo fifo;
    Sync syn;
    syn.OpenReadOrDie();

    //1.获取key
    //cout << "I am server" << endl;
    key_t key = GetKeyOrDie();
    cout << "server  key: " << ToHEX(key) << endl;
    // sleep(2);

    //2.创建共享内存块
    int shmid = CreateShmOrDie(key, DEFAULT_SIZE);
    cout << "server shmid: " << shmid << endl;
    // sleep(2);

    //3.构建映射
    // DebugShm(shmid);
    char* address = (char*)AttachShm(shmid);
    cout << "server attach done,  address : " << (void*)address << endl;

    while(1)
    {
        if(syn.Wait())
            cout << "message : " << address << endl;
        else
            break;
    }

    //4.移除映射
    DetachShm(address);
    cout << "server detach done, address : " << (void*)address << endl;

    //5.删除共享内存块
    sleep(10);
    cout << "destroy shared memoey..." << endl;
    DestroyShm(shmid);

    return 0;
}